package com.example.lyonlinemusic.controller;


import com.example.lyonlinemusic.mapper.LoveMusicMapper;
import com.example.lyonlinemusic.model.Music;
import com.example.lyonlinemusic.model.User;
import com.example.lyonlinemusic.tools.Constant;
import com.example.lyonlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired
    private LoveMusicMapper loveMusicMapper;


    //收藏音乐
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId=Integer.parseInt(id);
        System.out.println("musicId:"+musicId);
        //检查是否登录了
        HttpSession httpSession=request.getSession(false);
        if(httpSession==null||httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后再查询",false);
        }
        User user=(User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        System.out.println("userId:"+userId);

        Music music=loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music!=null){
            //之前收藏过，再点击就取消收藏
            int ret=loveMusicMapper.deleteLoveMusicByMusicIdAndUserId(userId,musicId);
            if(ret==1){
                return new ResponseBodyMessage<>(0,"取消收藏音乐成功！",true);
            }else{
                return new ResponseBodyMessage<>(-1,"取消收藏音乐失败！",false);
            }
        }else{
            boolean effect=loveMusicMapper.insertLoveMusic(userId,musicId);
            if(effect){
                return new ResponseBodyMessage<>(0,"收藏成功！",true);
            }else{
                return new ResponseBodyMessage<>(-1,"收藏失败！",false);
            }
        }
    }



    //查询收藏过的音乐
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false)String musicName ,HttpServletRequest request){
        //检查是否登录了
        HttpSession httpSession=request.getSession(false);
        if(httpSession==null||httpSession.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"请登录后再查询！",null);
        }
        User user=(User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        System.out.println("userId:"+userId);

        List<Music> musicList=null;
        if(musicName==null){
            musicList=loveMusicMapper.findLoveMusicByUserId(userId);
        }else{
            musicList=loveMusicMapper.findLoveMusicByMusicNameAndUserID(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);
    }

}
